package com.ruoyi.web.controller.wechat;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wechat.domain.WxMsgReplyForm;
import com.ruoyi.wechat.domain.WxWxMsg;
import com.ruoyi.wechat.service.MsgReplyService;
import com.ruoyi.wechat.service.WxWxMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


/**
 * 微信消息
 *
 * @author niefy
 * @date 2020-05-14 17:28:34
 */
@Slf4j
@RestController
@RequestMapping("/manage/wxMsg")
@Api(tags = {"公众号消息记录-用户消息"})
public class WxMsgManageController {
    @Autowired
    private WxWxMsgService wxMsgService;
    @Autowired
    private MsgReplyService msgReplyService;

    @Autowired
    private WxMpService wxMpService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public AjaxResult list(Page page, WxWxMsg wxWxMsgVo) {
        log.info("查询条件:{}", wxWxMsgVo);
        return AjaxResult.success(wxMsgService.selectWxMsgList(page, wxWxMsgVo));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "详情")
    public AjaxResult info(String appid, @PathVariable("id") Long id) {
        WxWxMsg wxMsg = wxMsgService.getById(id);
        return AjaxResult.success("wxMsg", wxMsg);
    }

    /**
     * 回复
     */
    @PostMapping("/reply")
    @ApiOperation(value = "回复")
    public AjaxResult reply(String appid, @RequestBody WxMsgReplyForm form) {
        log.info("回复参数：{}", form);
        wxMpService.switchover(appid);
        msgReplyService.reply(form.getOpenid(), form.getReplyType(), form.getReplyContent());
        return AjaxResult.success();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@RequestParam String appid, @RequestBody Long[] ids) {
        wxMsgService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

}
